﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;

namespace IGGWindowApp.Panels
{
    public partial class PanelGameNavigator : UserControl
    {
        // EVENTS
        public event EventHandler NextPanelClicked;
        public delegate void GameSelectedHandler(Game game);
        public event GameSelectedHandler GameSelected;

        private List<Boxscore> m_boxPanels;
        private Boxscore m_lastSelected = null;

        public PanelGameNavigator()
        {
            InitializeComponent();
            m_boxPanels = new List<Boxscore>();
            InitializeSixteenBoxes();
        }

        private void InitializeSixteenBoxes()
        {
            bool pair = true;
            for (int i = 0; i < 16; i++)
            {
                Boxscore panel = new Boxscore();
                panel.Initialize();
                if (pair)
                {
                    pair = false;
                    panel.Margin = new Padding(0, 3, 3, 3);
                }
                else
                {
                    pair = true;
                    panel.Margin = new Padding(3, 3, 0, 3);
                }
                
                m_boxPanels.Add(panel);
                tableLayoutPanel.Controls.Add(panel);
                panel.boxSelected += new Boxscore.BoxSelectionHandler(panel_boxSelected);
            }
        }

        private void panel_boxSelected(PanelGameNavigator.Boxscore sender, Game game)
        {
            if (m_lastSelected!=null)
            {
                m_lastSelected.ResetColors();
            }
            m_lastSelected = sender;
        }

        void panel_MouseLeave(object sender, EventArgs e)
        {
            ((Panel)sender).BackColor = Color.LightGray;
        }

        void panel_MouseHover(object sender, EventArgs e)
        {
            ((Panel)sender).BackColor = Color.WhiteSmoke;
        }

        /// <summary>
        /// Set the game list boxes
        /// </summary>
        public void SetData(Scoreboard board)
        {
            int count = board.GamesList.Games.Count;
            if (count == 0)
            {
                this.Controls.Remove(tableLayoutPanel);
                Label labelNoGames = new Label();
                labelNoGames.AutoSize = true;
                labelNoGames.Location = new System.Drawing.Point(6, 14);
                labelNoGames.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelNoGames.Size = new System.Drawing.Size(93, 13);
                labelNoGames.TabIndex = 0;
                labelNoGames.Text = "No games today.";
                this.Controls.Add(labelNoGames);
                this.Refresh();
            }
            else if (count > 16)
            {
                count = 16;
            }
            for (int i = 0; i < count; i++)
            {
                m_boxPanels[i].Visible = true;
                m_boxPanels[i].UpdateData(board.GamesList.Games[i]);
                m_boxPanels[i].boxSelected += new Boxscore.BoxSelectionHandler(PanelGameNavigator_boxSelected);
            }
        }

        private void PanelGameNavigator_boxSelected(PanelGameNavigator.Boxscore sender, Game game)
        {
            GameSelected(game);
        }

        private class Boxscore : Panel
        {
            public delegate void BoxSelectionHandler(Boxscore sender, Game game);
            public event BoxSelectionHandler boxSelected;

            private Color HIGHLIGHT = SystemColors.ControlDarkDark;
            private Game m_boxGame = null;

            private Label labelStatus;
            private Label labelGameSummary;
            private Label labelTeam2Score;
            private Label labelTeam2Name;
            private Label labelTeam1Score;
            private Label labelTeam1Name;
            private Panel panelHome;
            private Panel panelAway;

            public Game boxGame
            {
                get
                {
                    return m_boxGame;
                }
            }

            /// <summary>
            /// Update the boxscore fields
            /// </summary>
            public void UpdateData(Game game)
            {
                if (game == null)
                {
                    this.Visible = false;
                }
                else
                {
                    m_boxGame = game;

                    this.Visible = true;
                    labelTeam1Name.Text = game.AwayTeam.Abbrv.ToUpper();
                    labelTeam2Name.Text = game.HomeTeam.Abbrv.ToUpper();
                    //labelTeam1Stats.Text = "(" + game.AwayTeam.Wins.ToString() + "-" + game.AwayTeam.Losses.ToString() + ")";
                    //labelTeam2Stats.Text = "(" + game.HomeTeam.Wins.ToString() + "-" + game.HomeTeam.Losses.ToString() + ")";
                    labelTeam1Score.Text = game.AwayScore.ToString();
                    labelTeam2Score.Text = game.HomeScore.ToString();
                    if (game.Linescore != null)
                    {
                        //labelTeam1Hits.Text = game.Linescore.GetAwayLineScore().Hits.ToString();
                        //labelTeam2Hits.Text = game.Linescore.GetHomeLineScore().Hits.ToString();
                        //labelTeam1Errors.Text = game.Linescore.GetAwayLineScore().Errors.ToString();
                        //labelTeam2Errors.Text = game.Linescore.GetHomeLineScore().Errors.ToString();
                    }
                    switch (game.Status.Indicator.Category)
                    {
                        case Indicator.IndicatorCategoy.TERMINATED:
                            labelStatus.Text = "F";
                            if (game.Status.WinningPitcher.LastName.Trim() != string.Empty)
                            {
                                labelGameSummary.Text = "W: " + game.Status.WinningPitcher.LastName
                                    + " (" + game.Status.WinningPitcher.Wins
                                    + "-" + game.Status.WinningPitcher.Losses
                                    + ")   ";
                            }
                            else
                            {
                                labelGameSummary.Text = "";
                            }
                            if (game.Status.SavingPitcher != null && game.Status.SavingPitcher.LastName != "")
                            {
                                //labelLine1.Text = "S: " + game.Status.SavingPitcher.LastName
                                 //   + " (" + game.Status.SavingPitcher.Saves + ")";
                            }
                            else
                            {
                                //labelLine1.Text = "";
                            }
                            //labelHR.Visible = true;
                            string hrstr = "";
                            if (game.Status.HomeRuns.Count > 0)
                            {
                                hrstr = "HR: ";
                                foreach (HomeRun hr in game.Status.HomeRuns)
                                {
                                    hrstr += hr.Player.LastName + ", ";
                                }
                                hrstr = hrstr.Substring(0, hrstr.Length - 2);
                                if (game.Status.SavingPitcher != null && game.Status.SavingPitcher.LastName != "")
                                {
                                    //labelHR.Text = hrstr;
                                }
                                else
                                {
                                    //labelLine1.Text = hrstr;
                                    //labelHR.Text = "";
                                }
                            }
                            else
                            {
                                //labelHR.Text = "";
                            }

                            break;
                        case Indicator.IndicatorCategoy.INTERRUPTED:
                            labelStatus.Text = "R";
                            break;
                        case Indicator.IndicatorCategoy.IN_PROGRESS:
                            labelStatus.Text = game.Status.Inning.ToString();
                            labelGameSummary.Text = "P: " + game.Status.CurrentPitcher.FullName;
                            if (game.Status.InningTop)
                            {
                                //labelTeam1Stats.Text += " *";
                            }
                            else
                            {
                                //labelTeam2Stats.Text += " *";
                            }
                            if (game.Status.HomeRuns.Count > 0)
                            {
                                hrstr = "HR: ";
                                foreach (HomeRun hr in game.Status.HomeRuns)
                                {
                                    hrstr += hr.Player.LastName + ", ";
                                }
                                hrstr = hrstr.Substring(0, hrstr.Length - 2);
                            }
                            break;
                        case Indicator.IndicatorCategoy.NEXT:
                            labelStatus.Text = game.StartTime;
                            if (game.AwayProbable != null && game.AwayProbable.Name.Trim() != string.Empty)
                            {
                                string[] names = game.AwayProbable.Name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                labelGameSummary.Text = names[names.Length-1];
                                names = game.HomeProbable.Name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                labelGameSummary.Text += " / " + names[names.Length - 1];
                            }
                            else
                            {
                                labelGameSummary.Text = "";
                            }
                            break;
                        case Indicator.IndicatorCategoy.WARMUP:
                            labelStatus.Text = game.StartTime;
                            if (game.AwayProbable != null)
                            {
                                labelGameSummary.Text = game.AwayProbable.Name;
                            }
                            else
                            {
                                labelGameSummary.Text = "";
                            }
                            break;
                    }
                }
            }

            public void Initialize()
            {
                string fontlb = "Segoe UI";

                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.BackColor = SystemColors.Control;
                this.Size = new System.Drawing.Size(215, 82);
                this.Visible = false;
                this.Margin = new Padding(0, 3, 3, 3);

                labelStatus = new Label();
                labelStatus.Font = new System.Drawing.Font(fontlb, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelStatus.Location = new System.Drawing.Point(64, 7);
                labelStatus.Size = new System.Drawing.Size(58, 18);
                labelStatus.Text = "Bot 6";
                labelStatus.TextAlign = System.Drawing.ContentAlignment.TopRight;
                this.Controls.Add(labelStatus);

                labelGameSummary = new Label();
                labelGameSummary.AutoSize = false;
                labelGameSummary.Font = new System.Drawing.Font(fontlb, 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelGameSummary.Location = new System.Drawing.Point(1, 33);
                labelGameSummary.Size = new System.Drawing.Size(155, 14);
                labelGameSummary.Text = "W: Romero (6-3)";
                this.Controls.Add(labelGameSummary);

                panelHome = new Panel();
                panelHome.Dock = System.Windows.Forms.DockStyle.Top;
                panelHome.Location = new System.Drawing.Point(0, 20);
                panelHome.Size = new System.Drawing.Size(50, 16);
                this.Controls.Add(panelHome);

                labelTeam2Score = new Label();
                labelTeam2Score.AutoSize = true;
                labelTeam2Score.Font = new System.Drawing.Font(fontlb, 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Score.Location = new System.Drawing.Point(40, 1);
                labelTeam2Score.Size = new System.Drawing.Size(14, 15);
                labelTeam2Score.Text = "3";
                panelHome.Controls.Add(labelTeam2Score);

                labelTeam2Name = new Label();
                labelTeam2Name.AutoSize = true;
                labelTeam2Name.Font = new System.Drawing.Font(fontlb, 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam2Name.Location = new System.Drawing.Point(0, 1);
                labelTeam2Name.Size = new System.Drawing.Size(29, 15);
                labelTeam2Name.Text = "CLE";
                panelHome.Controls.Add(labelTeam2Name);

                panelAway = new Panel();
                panelAway.Dock = System.Windows.Forms.DockStyle.Top;
                panelAway.Location = new System.Drawing.Point(0, 0);
                panelAway.Size = new System.Drawing.Size(50, 14);
                this.Controls.Add(panelAway);

                labelTeam1Score = new Label();
                labelTeam1Score.AutoSize = true;
                labelTeam1Score.Font = new System.Drawing.Font(fontlb, 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Score.Location = new System.Drawing.Point(40, 0);
                labelTeam1Score.Size = new System.Drawing.Size(14, 15);
                labelTeam1Score.Text = "0";
                panelAway.Controls.Add(labelTeam1Score);

                labelTeam1Name = new Label();
                labelTeam1Name.AutoSize = true;
                labelTeam1Name.Font = new System.Drawing.Font(fontlb,9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labelTeam1Name.Location = new System.Drawing.Point(0, 0);
                labelTeam1Name.Size = new System.Drawing.Size(26, 15);
                labelTeam1Name.Text = "SD";
                panelAway.Controls.Add(labelTeam1Name);

                this.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Name.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam1Score.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                panelAway.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Name.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelTeam2Score.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                panelHome.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelGameSummary.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
                labelStatus.MouseClick += new MouseEventHandler(Boxscore_MouseClick);
            }

            private void Boxscore_MouseClick(object sender, MouseEventArgs e)
            {
                this.BackColor = HIGHLIGHT;
                this.ForeColor = SystemColors.Control;
                labelTeam1Name.BackColor = HIGHLIGHT;
                labelTeam1Score.BackColor = HIGHLIGHT;
                panelAway.BackColor = HIGHLIGHT;
                labelTeam2Name.BackColor = HIGHLIGHT;
                labelTeam2Score.BackColor = HIGHLIGHT;
                panelHome.BackColor = HIGHLIGHT;
                labelGameSummary.BackColor = HIGHLIGHT;
                labelStatus.BackColor = HIGHLIGHT;
                this.Refresh();
                boxSelected(this, m_boxGame);
            }

            public void ResetColors()
            {
                this.BackColor = SystemColors.Control;
                this.ForeColor = Color.Black;
                labelTeam1Name.BackColor = SystemColors.Control;
                labelTeam1Score.BackColor = SystemColors.Control;
                panelAway.BackColor = SystemColors.Control;
                labelTeam2Name.BackColor = SystemColors.Control;
                labelTeam2Score.BackColor = SystemColors.Control;
                panelHome.BackColor = SystemColors.Control;
                labelGameSummary.BackColor = SystemColors.Control;
                labelStatus.BackColor = SystemColors.Control;
            }
        }

        private void pctNext_Click(object sender, EventArgs e)
        {
            NextPanelClicked(this, null);
        }

    }

}
